﻿# region
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Runtime.InteropServices;//捕捉错误
# endregion

# region Using ArcGIS
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.esriSystem;//不用从引用加入
using ESRI.ArcGIS.Carto;//管理图层
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Display;
# endregion



namespace popGis
{
    public class JoinTable
    {
        /// <summary>
        /// 将要素图层与外表进行关联
        /// </summary>
        /// <param name="pWorkspace">定义工作空间</param>
        /// <param name="pFeatureLayer">要素图层</param>
        /// <param name="tableName">外表名称</param>
        /// <param name="sPrimaryField">图层关联名字</param>
        /// <param name="sForeignField">外表关联字段</param>
        /// <returns>关联成功返回true，关联失败或无法关联返回false</returns>
        static public bool JoinData(IWorkspace pWorkspace,IFeatureLayer pFeatureLayer, string tableName, string sPrimaryField, string sForeignField,esriJoinType jointype)
        {
            bool tableJoined;
            //删除所有连接
            (pFeatureLayer as IDisplayRelationshipClass).DisplayRelationshipClass(null, esriJoinType.esriLeftOuterJoin); 
            tableJoined = false;

            IFeatureWorkspace               pFeatureWorkspace;
            IFeatureClass                   pFeatureClass;
            ITable                          pPrimaryTable;
            ITable                          pForeignTable;
            IMemoryRelationshipClassFactory pMemoryRelationshipCF;
            IRelationshipClass              pRelationshipClass;
            IDisplayTable                   pDisplayTable;
            IDisplayRelationshipClass       pDisplayRelationshipC;

            //设置工作空间
            pFeatureWorkspace = (IFeatureWorkspace)pWorkspace;
            //打开外表 -- ITable
            pForeignTable = pFeatureWorkspace.OpenTable(tableName);
            //获得要素图层的要素表
            pDisplayTable = (IDisplayTable)pFeatureLayer;
            pFeatureClass = (IFeatureClass)pDisplayTable.DisplayTable;
            pPrimaryTable = (ITable)pFeatureClass;
            //创建内存关系类工厂
            pMemoryRelationshipCF = new MemoryRelationshipClassFactoryClass();

            try
            {
                //全代码最长的一条语句
                pRelationshipClass = pMemoryRelationshipCF.Open("MemoryRelationship", (IObjectClass)pPrimaryTable, sPrimaryField,
                    (IObjectClass)pForeignTable, sForeignField, "forward", "backward", esriRelCardinality.esriRelCardinalityOneToOne);
                pDisplayRelationshipC = (IDisplayRelationshipClass)pFeatureLayer;

                if (jointype == esriJoinType.esriLeftOuterJoin)
                    pDisplayRelationshipC.DisplayRelationshipClass(pRelationshipClass, esriJoinType.esriLeftOuterJoin);//所有源保留
                else
                    //默认匹配方式，保留匹配的源
                    pDisplayRelationshipC.DisplayRelationshipClass(pRelationshipClass, esriJoinType.esriLeftInnerJoin);

                tableJoined = true;
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message, "关联图层失败：");
            }

            return tableJoined;
        }
        ///重载，以匹配关联方式关联
        static public bool JoinData(IWorkspace pWorkspace, IFeatureLayer pFeatureLayer, string tableName, string sPrimaryField, string sForeignField)
        {
            return JoinData(pWorkspace, pFeatureLayer, tableName, sPrimaryField, sForeignField, esriJoinType.esriLeftInnerJoin);
        }


        /// <summary>
        /// 删除要素图层所有连接
        /// </summary>
        /// <param name="pFeatureLayer"></param>
        static public void setFeatureLayerRelationshipNull(IFeatureLayer pFeatureLayer)
        {
            //将 RelationshipClass 设为 null，赋给 DisplayRelationshipClass 即可
            (pFeatureLayer as IDisplayRelationshipClass).DisplayRelationshipClass(null, esriJoinType.esriLeftOuterJoin);
        }
    }
}

